1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.URI;
26 
27 private import glib.ConstructionException;
28 private import glib.HashTable;
29 private import glib.MemorySlice;
30 private import glib.Str;
31 private import glib.c.functions;
32 private import gobject.ObjectG;
33 private import linker.Loader;
34 private import soup.c.functions;
35 public  import soup.c.types;
36 
37 
38 /**
39  * A #SoupURI represents a (parsed) URI. #SoupURI supports RFC 3986
40  * (URI Generic Syntax), and can parse any valid URI. However, libsoup
41  * only uses "http" and "https" URIs internally; You can use
42  * SOUP_URI_VALID_FOR_HTTP() to test if a #SoupURI is a valid HTTP
43  * URI.
44  * 
45  * @scheme will always be set in any URI. It is an interned string and
46  * is always all lowercase. (If you parse a URI with a non-lowercase
47  * scheme, it will be converted to lowercase.) The macros
48  * %SOUP_URI_SCHEME_HTTP and %SOUP_URI_SCHEME_HTTPS provide the
49  * interned values for "http" and "https" and can be compared against
50  * URI @scheme values.
51  * 
52  * @user and @password are parsed as defined in the older URI specs
53  * (ie, separated by a colon; RFC 3986 only talks about a single
54  * "userinfo" field). Note that @password is not included in the
55  * output of soup_uri_to_string(). libsoup does not normally use these
56  * fields; authentication is handled via #SoupSession signals.
57  * 
58  * @host contains the hostname, and @port the port specified in the
59  * URI. If the URI doesn't contain a hostname, @host will be %NULL,
60  * and if it doesn't specify a port, @port may be 0. However, for
61  * "http" and "https" URIs, @host is guaranteed to be non-%NULL
62  * (trying to parse an http URI with no @host will return %NULL), and
63  * @port will always be non-0 (because libsoup knows the default value
64  * to use when it is not specified in the URI).
65  * 
66  * @path is always non-%NULL. For http/https URIs, @path will never be
67  * an empty string either; if the input URI has no path, the parsed
68  * #SoupURI will have a @path of "/".
69  * 
70  * @query and @fragment are optional for all URI types.
71  * soup_form_decode() may be useful for parsing @query.
72  * 
73  * Note that @path, @query, and @fragment may contain
74  * %<!-- -->-encoded characters. soup_uri_new() calls
75  * soup_uri_normalize() on them, but not soup_uri_decode(). This is
76  * necessary to ensure that soup_uri_to_string() will generate a URI
77  * that has exactly the same meaning as the original. (In theory,
78  * #SoupURI should leave @user, @password, and @host partially-encoded
79  * as well, but this would be more annoying than useful.)
80  */
81 public final class URI
82 {
83 	/** the main Gtk struct */
84 	protected SoupURI* soupURI;
85 	protected bool ownedRef;
86 
87 	/** Get the main Gtk struct */
88 	public SoupURI* getURIStruct(bool transferOwnership = false)
89 	{
90 		if (transferOwnership)
91 			ownedRef = false;
92 		return soupURI;
93 	}
94 
95 	/** the main Gtk struct as a void* */
96 	protected void* getStruct()
97 	{
98 		return cast(void*)soupURI;
99 	}
100 
101 	/**
102 	 * Sets our main struct and passes it to the parent class.
103 	 */
104 	public this (SoupURI* soupURI, bool ownedRef = false)
105 	{
106 		this.soupURI = soupURI;
107 		this.ownedRef = ownedRef;
108 	}
109 
110 	~this ()
111 	{
112 		if ( Linker.isLoaded(LIBRARY_SOUP[0]) && ownedRef )
113 			soup_uri_free(soupURI);
114 	}
115 
116 
117 	/**
118 	 * the URI scheme (eg, "http")
119 	 */
120 	public @property string scheme()
121 	{
122 		return Str.toString(soupURI.scheme);
123 	}
124 
125 	/** Ditto */
126 	public @property void scheme(string value)
127 	{
128 		soupURI.scheme = Str.toStringz(value);
129 	}
130 
131 	/**
132 	 * a username, or %NULL
133 	 */
134 	public @property string user()
135 	{
136 		return Str.toString(soupURI.user);
137 	}
138 
139 	/** Ditto */
140 	public @property void user(string value)
141 	{
142 		soupURI.user = Str.toStringz(value);
143 	}
144 
145 	/**
146 	 * a password, or %NULL
147 	 */
148 	public @property string password()
149 	{
150 		return Str.toString(soupURI.password);
151 	}
152 
153 	/** Ditto */
154 	public @property void password(string value)
155 	{
156 		soupURI.password = Str.toStringz(value);
157 	}
158 
159 	/**
160 	 * the hostname or IP address, or %NULL
161 	 */
162 	public @property string host()
163 	{
164 		return Str.toString(soupURI.host);
165 	}
166 
167 	/** Ditto */
168 	public @property void host(string value)
169 	{
170 		soupURI.host = Str.toStringz(value);
171 	}
172 
173 	/**
174 	 * the port number on @host
175 	 */
176 	public @property uint port()
177 	{
178 		return soupURI.port;
179 	}
180 
181 	/** Ditto */
182 	public @property void port(uint value)
183 	{
184 		soupURI.port = value;
185 	}
186 
187 	/**
188 	 * the path on @host
189 	 */
190 	public @property string path()
191 	{
192 		return Str.toString(soupURI.path);
193 	}
194 
195 	/** Ditto */
196 	public @property void path(string value)
197 	{
198 		soupURI.path = Str.toStringz(value);
199 	}
200 
201 	/**
202 	 * a query for @path, or %NULL
203 	 */
204 	public @property string query()
205 	{
206 		return Str.toString(soupURI.query);
207 	}
208 
209 	/** Ditto */
210 	public @property void query(string value)
211 	{
212 		soupURI.query = Str.toStringz(value);
213 	}
214 
215 	/**
216 	 * a fragment identifier within @path, or %NULL
217 	 */
218 	public @property string fragment()
219 	{
220 		return Str.toString(soupURI.fragment);
221 	}
222 
223 	/** Ditto */
224 	public @property void fragment(string value)
225 	{
226 		soupURI.fragment = Str.toStringz(value);
227 	}
228 
229 	/** */
230 	public static GType getType()
231 	{
232 		return soup_uri_get_type();
233 	}
234 
235 	/**
236 	 * Parses an absolute URI.
237 	 *
238 	 * You can also pass %NULL for @uri_string if you want to get back an
239 	 * "empty" #SoupURI that you can fill in by hand. (You will need to
240 	 * call at least soup_uri_set_scheme() and soup_uri_set_path(), since
241 	 * those fields are required.)
242 	 *
243 	 * Params:
244 	 *     uriString = a URI
245 	 *
246 	 * Returns: a #SoupURI, or %NULL if the given string
247 	 *     was found to be invalid.
248 	 *
249 	 * Throws: ConstructionException GTK+ fails to create the object.
250 	 */
251 	public this(string uriString)
252 	{
253 		auto __p = soup_uri_new(Str.toStringz(uriString));
254 
255 		if(__p is null)
256 		{
257 			throw new ConstructionException("null returned by new");
258 		}
259 
260 		this(cast(SoupURI*) __p);
261 	}
262 
263 	/**
264 	 * Parses @uri_string relative to @base.
265 	 *
266 	 * Params:
267 	 *     base = a base URI
268 	 *     uriString = the URI
269 	 *
270 	 * Returns: a parsed #SoupURI.
271 	 *
272 	 * Throws: ConstructionException GTK+ fails to create the object.
273 	 */
274 	public this(URI base, string uriString)
275 	{
276 		auto __p = soup_uri_new_with_base((base is null) ? null : base.getURIStruct(), Str.toStringz(uriString));
277 
278 		if(__p is null)
279 		{
280 			throw new ConstructionException("null returned by new_with_base");
281 		}
282 
283 		this(cast(SoupURI*) __p);
284 	}
285 
286 	/**
287 	 * Copies @uri
288 	 *
289 	 * Returns: a copy of @uri, which must be freed with soup_uri_free()
290 	 */
291 	public URI copy()
292 	{
293 		auto __p = soup_uri_copy(soupURI);
294 
295 		if(__p is null)
296 		{
297 			return null;
298 		}
299 
300 		return ObjectG.getDObject!(URI)(cast(SoupURI*) __p, true);
301 	}
302 
303 	/**
304 	 * Makes a copy of @uri, considering only the protocol, host, and port
305 	 *
306 	 * Returns: the new #SoupURI
307 	 *
308 	 * Since: 2.28
309 	 */
310 	public URI copyHost()
311 	{
312 		auto __p = soup_uri_copy_host(soupURI);
313 
314 		if(__p is null)
315 		{
316 			return null;
317 		}
318 
319 		return ObjectG.getDObject!(URI)(cast(SoupURI*) __p, true);
320 	}
321 
322 	/**
323 	 * Tests whether or not @uri1 and @uri2 are equal in all parts
324 	 *
325 	 * Params:
326 	 *     uri2 = another #SoupURI
327 	 *
328 	 * Returns: %TRUE or %FALSE
329 	 */
330 	public bool equal(URI uri2)
331 	{
332 		return soup_uri_equal(soupURI, (uri2 is null) ? null : uri2.getURIStruct()) != 0;
333 	}
334 
335 	/**
336 	 * Frees @uri.
337 	 */
338 	public void free()
339 	{
340 		soup_uri_free(soupURI);
341 		ownedRef = false;
342 	}
343 
344 	/**
345 	 * Gets @uri's fragment.
346 	 *
347 	 * Returns: @uri's fragment.
348 	 *
349 	 * Since: 2.32
350 	 */
351 	public string getFragment()
352 	{
353 		return Str.toString(soup_uri_get_fragment(soupURI));
354 	}
355 
356 	/**
357 	 * Gets @uri's host.
358 	 *
359 	 * Returns: @uri's host.
360 	 *
361 	 * Since: 2.32
362 	 */
363 	public string getHost()
364 	{
365 		return Str.toString(soup_uri_get_host(soupURI));
366 	}
367 
368 	/**
369 	 * Gets @uri's password.
370 	 *
371 	 * Returns: @uri's password.
372 	 *
373 	 * Since: 2.32
374 	 */
375 	public string getPassword()
376 	{
377 		return Str.toString(soup_uri_get_password(soupURI));
378 	}
379 
380 	/**
381 	 * Gets @uri's path.
382 	 *
383 	 * Returns: @uri's path.
384 	 *
385 	 * Since: 2.32
386 	 */
387 	public string getPath()
388 	{
389 		return Str.toString(soup_uri_get_path(soupURI));
390 	}
391 
392 	/**
393 	 * Gets @uri's port.
394 	 *
395 	 * Returns: @uri's port.
396 	 *
397 	 * Since: 2.32
398 	 */
399 	public uint getPort()
400 	{
401 		return soup_uri_get_port(soupURI);
402 	}
403 
404 	/**
405 	 * Gets @uri's query.
406 	 *
407 	 * Returns: @uri's query.
408 	 *
409 	 * Since: 2.32
410 	 */
411 	public string getQuery()
412 	{
413 		return Str.toString(soup_uri_get_query(soupURI));
414 	}
415 
416 	/**
417 	 * Gets @uri's scheme.
418 	 *
419 	 * Returns: @uri's scheme.
420 	 *
421 	 * Since: 2.32
422 	 */
423 	public string getScheme()
424 	{
425 		return Str.toString(soup_uri_get_scheme(soupURI));
426 	}
427 
428 	/**
429 	 * Gets @uri's user.
430 	 *
431 	 * Returns: @uri's user.
432 	 *
433 	 * Since: 2.32
434 	 */
435 	public string getUser()
436 	{
437 		return Str.toString(soup_uri_get_user(soupURI));
438 	}
439 
440 	/**
441 	 * Compares @v1 and @v2, considering only the scheme, host, and port.
442 	 *
443 	 * Params:
444 	 *     v2 = a #SoupURI with a non-%NULL @host member
445 	 *
446 	 * Returns: whether or not the URIs are equal in scheme, host,
447 	 *     and port.
448 	 *
449 	 * Since: 2.28
450 	 */
451 	public bool hostEqual(URI v2)
452 	{
453 		return soup_uri_host_equal(soupURI, (v2 is null) ? null : v2.getURIStruct()) != 0;
454 	}
455 
456 	/**
457 	 * Hashes @key, considering only the scheme, host, and port.
458 	 *
459 	 * Returns: a hash
460 	 *
461 	 * Since: 2.28
462 	 */
463 	public uint hostHash()
464 	{
465 		return soup_uri_host_hash(soupURI);
466 	}
467 
468 	/**
469 	 * Sets @uri's fragment to @fragment.
470 	 *
471 	 * Params:
472 	 *     fragment = the fragment
473 	 */
474 	public void setFragment(string fragment)
475 	{
476 		soup_uri_set_fragment(soupURI, Str.toStringz(fragment));
477 	}
478 
479 	/**
480 	 * Sets @uri's host to @host.
481 	 *
482 	 * If @host is an IPv6 IP address, it should not include the brackets
483 	 * required by the URI syntax; they will be added automatically when
484 	 * converting @uri to a string.
485 	 *
486 	 * http and https URIs should not have a %NULL @host.
487 	 *
488 	 * Params:
489 	 *     host = the hostname or IP address, or %NULL
490 	 */
491 	public void setHost(string host)
492 	{
493 		soup_uri_set_host(soupURI, Str.toStringz(host));
494 	}
495 
496 	/**
497 	 * Sets @uri's password to @password.
498 	 *
499 	 * Params:
500 	 *     password = the password, or %NULL
501 	 */
502 	public void setPassword(string password)
503 	{
504 		soup_uri_set_password(soupURI, Str.toStringz(password));
505 	}
506 
507 	/**
508 	 * Sets @uri's path to @path.
509 	 *
510 	 * Params:
511 	 *     path = the non-%NULL path
512 	 */
513 	public void setPath(string path)
514 	{
515 		soup_uri_set_path(soupURI, Str.toStringz(path));
516 	}
517 
518 	/**
519 	 * Sets @uri's port to @port. If @port is 0, @uri will not have an
520 	 * explicitly-specified port.
521 	 *
522 	 * Params:
523 	 *     port = the port, or 0
524 	 */
525 	public void setPort(uint port)
526 	{
527 		soup_uri_set_port(soupURI, port);
528 	}
529 
530 	/**
531 	 * Sets @uri's query to @query.
532 	 *
533 	 * Params:
534 	 *     query = the query
535 	 */
536 	public void setQuery(string query)
537 	{
538 		soup_uri_set_query(soupURI, Str.toStringz(query));
539 	}
540 
541 	/**
542 	 * Sets @uri's query to the result of encoding @form according to the
543 	 * HTML form rules. See soup_form_encode_hash() for more information.
544 	 *
545 	 * Params:
546 	 *     form = a #GHashTable containing HTML form
547 	 *         information
548 	 */
549 	public void setQueryFromForm(HashTable form)
550 	{
551 		soup_uri_set_query_from_form(soupURI, (form is null) ? null : form.getHashTableStruct());
552 	}
553 
554 	/**
555 	 * Sets @uri's scheme to @scheme. This will also set @uri's port to
556 	 * the default port for @scheme, if known.
557 	 *
558 	 * Params:
559 	 *     scheme = the URI scheme
560 	 */
561 	public void setScheme(string scheme)
562 	{
563 		soup_uri_set_scheme(soupURI, Str.toStringz(scheme));
564 	}
565 
566 	/**
567 	 * Sets @uri's user to @user.
568 	 *
569 	 * Params:
570 	 *     user = the username, or %NULL
571 	 */
572 	public void setUser(string user)
573 	{
574 		soup_uri_set_user(soupURI, Str.toStringz(user));
575 	}
576 
577 	/**
578 	 * Returns a string representing @uri.
579 	 *
580 	 * If @just_path_and_query is %TRUE, this concatenates the path and query
581 	 * together. That is, it constructs the string that would be needed in
582 	 * the Request-Line of an HTTP request for @uri.
583 	 *
584 	 * Note that the output will never contain a password, even if @uri
585 	 * does.
586 	 *
587 	 * Params:
588 	 *     justPathAndQuery = if %TRUE, output just the path and query portions
589 	 *
590 	 * Returns: a string representing @uri, which the caller must free.
591 	 */
592 	public string toString(bool justPathAndQuery)
593 	{
594 		auto retStr = soup_uri_to_string(soupURI, justPathAndQuery);
595 
596 		scope(exit) Str.freeString(retStr);
597 		return Str.toString(retStr);
598 	}
599 
600 	/**
601 	 * Tests if @uri uses the default port for its scheme. (Eg, 80 for
602 	 * http.) (This only works for http, https and ftp; libsoup does not know
603 	 * the default ports of other protocols.)
604 	 *
605 	 * Returns: %TRUE or %FALSE
606 	 */
607 	public bool usesDefaultPort()
608 	{
609 		return soup_uri_uses_default_port(soupURI) != 0;
610 	}
611 
612 	/**
613 	 * Fully %<!-- -->-decodes @part.
614 	 *
615 	 * In the past, this would return %NULL if @part contained invalid
616 	 * percent-encoding, but now it just ignores the problem (as
617 	 * soup_uri_new() already did).
618 	 *
619 	 * Params:
620 	 *     part = a URI part
621 	 *
622 	 * Returns: the decoded URI part.
623 	 */
624 	public static string decode(string part)
625 	{
626 		auto retStr = soup_uri_decode(Str.toStringz(part));
627 
628 		scope(exit) Str.freeString(retStr);
629 		return Str.toString(retStr);
630 	}
631 
632 	/**
633 	 * This %<!-- -->-encodes the given URI part and returns the escaped
634 	 * version in allocated memory, which the caller must free when it is
635 	 * done.
636 	 *
637 	 * Params:
638 	 *     part = a URI part
639 	 *     escapeExtra = additional reserved characters to
640 	 *         escape (or %NULL)
641 	 *
642 	 * Returns: the encoded URI part
643 	 */
644 	public static string encode(string part, string escapeExtra)
645 	{
646 		auto retStr = soup_uri_encode(Str.toStringz(part), Str.toStringz(escapeExtra));
647 
648 		scope(exit) Str.freeString(retStr);
649 		return Str.toString(retStr);
650 	}
651 
652 	/**
653 	 * %<!-- -->-decodes any "unreserved" characters (or characters in
654 	 * @unescape_extra) in @part, and %<!-- -->-encodes any non-ASCII
655 	 * characters, spaces, and non-printing characters in @part.
656 	 *
657 	 * "Unreserved" characters are those that are not allowed to be used
658 	 * for punctuation according to the URI spec. For example, letters are
659 	 * unreserved, so soup_uri_normalize() will turn
660 	 * <literal>http://example.com/foo/b%<!-- -->61r</literal> into
661 	 * <literal>http://example.com/foo/bar</literal>, which is guaranteed
662 	 * to mean the same thing. However, "/" is "reserved", so
663 	 * <literal>http://example.com/foo%<!-- -->2Fbar</literal> would not
664 	 * be changed, because it might mean something different to the
665 	 * server.
666 	 *
667 	 * In the past, this would return %NULL if @part contained invalid
668 	 * percent-encoding, but now it just ignores the problem (as
669 	 * soup_uri_new() already did).
670 	 *
671 	 * Params:
672 	 *     part = a URI part
673 	 *     unescapeExtra = reserved characters to unescape (or %NULL)
674 	 *
675 	 * Returns: the normalized URI part
676 	 */
677 	public static string normalize(string part, string unescapeExtra)
678 	{
679 		auto retStr = soup_uri_normalize(Str.toStringz(part), Str.toStringz(unescapeExtra));
680 
681 		scope(exit) Str.freeString(retStr);
682 		return Str.toString(retStr);
683 	}
684 }